package com.dynamixsoftware.printershare.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class XmlUtil {
	private static DocumentBuilderFactory docBuilderFactory = null;

	static {
		try {
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilderFactory.setValidating(false);
		} catch (Exception localException) {
			System.err.println("Cannot initialize XML parser. VERY BAD");
			localException.printStackTrace(System.err);
		}
	}

	public static final Element appendCDATA(Element paramElement,
			String paramString1, String paramString2) {
		Document localDocument = paramElement.getOwnerDocument();
		Element localElement = localDocument.createElement(paramString1);
		if (paramString2 != null) {
			localElement.appendChild(localDocument
					.createCDATASection(paramString2));
		}
		paramElement.appendChild(localElement);
		return localElement;
	}

	public static final Element appendElement(Element paramElement,
			String paramString) {
		return appendElement(paramElement, paramString, null);
	}

	public static final Element appendElement(Element paramElement,
			String paramString1, String paramString2) {
		Element localElement = paramElement.getOwnerDocument().createElement(
				paramString1);
		if (paramString2 != null) {
			createTextNode(localElement, paramString2);
		}
		paramElement.appendChild(localElement);
		return localElement;
	}

	public static final Node cloneAndAppend(Node paramNode1, Node paramNode2) {
		Document localDocument = paramNode2.getOwnerDocument();
		if (paramNode2.getNodeType() == 9) {
			localDocument = (Document) paramNode2;
		}
		if (localDocument != null) {
			Node localNode = localDocument.importNode(paramNode1, true);
			paramNode2.appendChild(localNode);
			return localNode;
		}
		return null;
	}

	public static final void createTextNode(Node paramNode, String paramString) {
		paramNode.appendChild(paramNode.getOwnerDocument().createTextNode(
				paramString));
	}

	public static final Document getDocument(File paramFile) throws Exception {
		return docBuilderFactory.newDocumentBuilder().parse(paramFile);
	}

	public static final Document getDocument(InputStream paramInputStream)
			throws Exception {
		return docBuilderFactory.newDocumentBuilder().parse(paramInputStream);
	}

	public static final Document getDocument(String paramString)
			throws Exception {
		return getDocument(new File(paramString));
	}

	public static final Element getElementById(Document paramDocument,
			String paramString1, String paramString2, String paramString3) {
		return getElementById(paramDocument.getDocumentElement(), paramString1,
				paramString2, paramString3);
	}

	public static final Element getElementById(Element paramElement,
			String paramString1, String paramString2, String paramString3) {
		NodeList localNodeList = paramElement
				.getElementsByTagName(paramString3);
		int i = localNodeList.getLength();
		for (int j = 0; j < i; j++) {
			Element localElement = (Element) localNodeList.item(j);
			if (localElement.getAttribute(paramString1).equals(paramString2)) {
				return localElement;
			}
		}
		return null;
	}

	public static final String getFirstCDATAValue(Element paramElement,
			String paramString) {
		Element localElement = getFirstElement(paramElement, paramString);
		if (localElement == null) {
			return "";
		}
		NodeList localNodeList = localElement.getChildNodes();
		int i = localNodeList.getLength();
		for (int j = 0; j < i; j++) {
			Node localNode = localNodeList.item(j);
			if (localNode.getNodeType() == 4) {
				return localNode.getNodeValue().trim();
			}
		}
		return "";
	}

	public static final Element getFirstElement(Node paramNode) {
		return getFirstElement(paramNode, null);
	}

	public static final Element getFirstElement(Node paramNode,
			String paramString) {
		if (paramNode == null) {
			return null;
		}
		NodeList localNodeList = paramNode.getChildNodes();
		if (localNodeList != null) {
			int i = localNodeList.getLength();
			int j = 0;
			if (j < i) {
				Node localNode = localNodeList.item(j);
				if (localNode.getNodeType() != 1) {
				}
				while ((paramString != null)
						&& (!paramString.equals(localNode.getNodeName()))) {
					j++;
					break;
				}
				return (Element) localNode;
			}
		}
		return null;
	}

	public static final Node getFirstNode(Node var0, String var1) {
		Node var5;
		if (var0 == null) {
			var5 = null;
			return var5;
		} else {
			NodeList var2 = var0.getChildNodes();
			if (var2 != null) {
				int var3 = var2.getLength();

				for (int var4 = 0; var4 < var3; ++var4) {
					var5 = var2.item(var4);
					if (var5.getNodeName().equals(var1)) {
						return var5;
					}
				}
			}

			return null;
		}
	}

	public static final String getFirstNodeAttribute(Node paramNode,
			String paramString1, String paramString2) {
		Node localNode1 = getFirstNode(paramNode, paramString1);
		if (localNode1 != null) {
		}
		for (Node localNode2 = localNode1.getFirstChild();; localNode2 = null) {
			Element localElement = (Element) localNode2;
			if (localElement == null) {
				break;
			}
			return localElement.getAttribute(paramString2);
		}
		return "";
	}

	public static final String getFirstNodeValue(Element paramElement,
			String paramString1, String paramString2) {
		String str = getFirstNodeValue(paramElement, paramString1);
		if (str.length() == 0) {
			str = paramString2;
		}
		return str;
	}

	public static final String getFirstNodeValue(Node paramNode,
			String paramString) {
		Element localElement = getFirstElement(paramNode, paramString);
		if (localElement != null) {
			return getNodeValue(localElement);
		}
		return "";
	}

	public static final int getFirstNodeValueInt(Element paramElement,
			String paramString, int paramInt) {
		String str = getFirstNodeValue(paramElement, paramString);
		if (str.length() != 0) {
		}
		try {
			int i = Integer.parseInt(str);
			paramInt = i;
			return paramInt;
		} catch (Exception localException) {
		}
		return paramInt;
	}

	public static final String getNodeValue(Node var0) {
		String var1;
		if (var0 == null) {
			var1 = "";
		} else {
			if (var0.getNodeType() == 1) {
				var0 = var0.getFirstChild();
			}

			if (var0 == null) {
				return "";
			}

			var1 = var0.getNodeValue();
			if (var1 == null) {
				return "";
			}
		}

		return var1;
	}

	public static final String[] getValues(Element paramElement,
			String paramString) {
		Vector localVector = new Vector();
		NodeList localNodeList = paramElement.getChildNodes();
		if (localNodeList != null) {
			int i = localNodeList.getLength();
			int j = 0;
			if (j < i) {
				Node localNode1 = localNodeList.item(j);
				if (localNode1.getNodeType() != 1) {
				}
				while ((paramString != null)
						&& (!paramString.equals(localNode1.getNodeName()))) {
					j++;
					break;
				}
				Node localNode2 = localNode1.getFirstChild();
				if (localNode2 == null) {
				}
				for (String str = "";; str = localNode2.getNodeValue()) {
					localVector.addElement(str);
					break;
				}
			}
		}
		String[] arrayOfString = new String[localVector.size()];
		localVector.copyInto(arrayOfString);
		return arrayOfString;
	}

	public static final Element mergeElements(Element var0, Element var1) {
	      NodeList var2 = var0.getChildNodes();
	      int var3 = var2.getLength();

	      for(int var4 = 0; var4 < var3; ++var4) {
	         Node var5 = var2.item(var4);
	         if(var5.getNodeType() == 1) {
	            Element var6 = (Element)var5;
	            Node var7 = getFirstNode(var1, var6.getNodeName());
	            if(var7 != null) {
	               var1.removeChild(var7);
	            }

	            cloneAndAppend(var6, var1);
	         }
	      }

	      return var1;
	   }

	public static final Document newDocument() throws Exception {
		return docBuilderFactory.newDocumentBuilder().newDocument();
	}

	public static final void removeElementsByTagName(Element paramElement,
			String paramString) {
		NodeList localNodeList = paramElement.getElementsByTagName(paramString);
		Vector localVector = new Vector();
		int i = localNodeList.getLength();
		for (int j = 0; j < i; j++) {
			localVector.add(localNodeList.item(j));
		}
		for (int k = 0; k < localVector.size(); k++) {
			paramElement.removeChild((Element) localVector.elementAt(k));
		}
	}

	public static final Node setFirstNodeValue(Element paramElement,
			String paramString1, String paramString2) {
		Node localNode1 = getFirstNode(paramElement, paramString1);
		if (localNode1 == null) {
			return appendElement(paramElement, paramString1, paramString2);
		}
		Node localNode2 = localNode1.getFirstChild();
		if (localNode2 == null) {
			createTextNode(localNode1, paramString2);
			return localNode1;
		}
		localNode2.setNodeValue(paramString2);
		return localNode1;
	}

	public static final void setNodeValue(Node var0, String var1) {
	      if(var0 != null) {
	         switch(var0.getNodeType()) {
	         case 1:
	            Node var2 = var0.getFirstChild();
	            if(var2 != null) {
	               var0.removeChild(var2);
	            }

	            if(var1 != null) {
	               createTextNode(var0, var1);
	            }
	         default:
	            if(var1 == null) {
	               var1 = "";
	            }

	            var0.setNodeValue(var1);
	         }
	      }
	   }

	public static final void writeDocument(Document paramDocument,
			File paramFile) throws Exception {
		FileOutputStream localFileOutputStream = new FileOutputStream(paramFile);
		writeDocument(paramDocument, localFileOutputStream);
		localFileOutputStream.close();
	}

	public static final void writeDocument(Document paramDocument,
			OutputStream paramOutputStream) throws Exception {
		OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(
				paramOutputStream, "UTF-8");
		writeDocument(paramDocument, localOutputStreamWriter);
		localOutputStreamWriter.flush();
	}

	public static final void writeDocument(Document paramDocument,
			Writer paramWriter) throws Exception {
		XmlUtil localXmlUtil = new XmlUtil();
		localXmlUtil.getClass();
		new XmlUtil.DOMSerializer(localXmlUtil).serialize(paramDocument, paramWriter);
	}

	public static final void writeElement(Element paramElement,
			OutputStream paramOutputStream) throws Exception {
		OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(
				paramOutputStream, "UTF-8");
		writeElement(paramElement, localOutputStreamWriter);
		localOutputStreamWriter.flush();
	}

	public static final void writeElement(Element paramElement,
			Writer paramWriter) throws Exception {
		XmlUtil localXmlUtil = new XmlUtil();
//		localXmlUtil.getClass();
		new DOMSerializer(localXmlUtil).serializeNode(paramElement,
				paramWriter, "");
	}

	class DOMSerializer {
		private String indent = "";
		private String lineSeparator = "\n";

		public DOMSerializer() {
		}
		public DOMSerializer(XmlUtil localXmlUtil) {
			// TODO Auto-generated constructor stub
		}

		protected String Normalize(String paramString) {
			if (paramString == null) {
				return "";
			}
			return paramString.replace("&", "&amp;").replace("\"", "&quot;")
					.replace("<", "&lt;").replace(">", "&gt;");
		}

		public void serialize(Document paramDocument, File paramFile)
				throws IOException {
			serialize(paramDocument, new FileWriter(paramFile));
		}

		public void serialize(Document paramDocument,
				OutputStream paramOutputStream) throws IOException {
			serialize(paramDocument, new OutputStreamWriter(paramOutputStream));
		}

		public void serialize(Document paramDocument, Writer paramWriter)
				throws IOException {
			serializeNode(paramDocument, paramWriter, "");
			paramWriter.flush();
		}

		 public void serializeNode(Node var1, Writer var2, String var3) throws IOException {
	         switch(var1.getNodeType()) {
	         case 1:
	            String var5 = var1.getNodeName();
	            var2.write(var3 + "<" + var5);
	            NamedNodeMap var6 = var1.getAttributes();

	            for(int var7 = 0; var7 < var6.getLength(); ++var7) {
	               Node var10 = var6.item(var7);
	               var2.write(" " + var10.getNodeName() + "=\"" + this.Normalize(var10.getNodeValue()) + "\"");
	            }

	            var2.write(">");
	            NodeList var8 = var1.getChildNodes();
	            if(var8 != null) {
	               if(var8.item(0) != null && var8.item(0).getNodeType() == 1) {
	                  var2.write(this.lineSeparator);
	               }

	               for(int var9 = 0; var9 < var8.getLength(); ++var9) {
	                  this.serializeNode(var8.item(var9), var2, var3 + this.indent);
	               }

	               if(var8.item(0) != null && var8.item(-1 + var8.getLength()).getNodeType() == 1) {
	                  var2.write(var3);
	               }
	            }

	            var2.write("</" + var5 + ">");
	            var2.write(this.lineSeparator);
	            return;
	         case 3:
	            var2.write(this.Normalize(var1.getNodeValue()));
	            return;
	         case 4:
	            var2.write("<![CDATA[" + var1.getNodeValue() + "]]>");
	            return;
	         case 5:
	            var2.write("&" + var1.getNodeName() + ";");
	            return;
	         case 7:
	            var2.write("<?" + var1.getNodeName() + " " + var1.getNodeValue() + "?>");
	            var2.write(this.lineSeparator);
	            return;
	         case 8:
	            var2.write(var3 + "<!-- " + var1.getNodeValue() + " -->");
	            var2.write(this.lineSeparator);
	            return;
	         case 9:
	            var2.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
	            var2.write(this.lineSeparator);
	            NodeList var11 = var1.getChildNodes();
	            if(var11 != null) {
	               for(int var12 = 0; var12 < var11.getLength(); ++var12) {
	                  this.serializeNode(var11.item(var12), var2, "");
	               }
	            }
	         case 2:
	         case 6:
	         default:
	            return;
	         case 10:
	            DocumentType var4 = (DocumentType)var1;
	            var2.write("<!DOCTYPE " + var4.getName());
	            if(var4.getPublicId() != null) {
	               System.out.print(" PUBLIC \"" + var4.getPublicId() + "\" ");
	            } else {
	               var2.write(" SYSTEM ");
	            }

	            var2.write("\"" + var4.getSystemId() + "\">");
	            var2.write(this.lineSeparator);
	         }
	      }


		public void setIndent(String paramString) {
			this.indent = paramString;
		}

		public void setLineSeparator(String paramString) {
			this.lineSeparator = paramString;
		}
	}
}

/*
 * Location:
 * C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar
 * .jar Qualified Name: com.dynamixsoftware.printershare.data.XmlUtil JD-Core
 * Version: 0.7.0.1
 */